/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * Jason this is the latest code V1.0
 */

package testmanagementclient2;

import edu.ipfw.cs590.cheat.services.utils.messages.ManagementMessage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;




/**
 *
 * @author rrm46582
 */
public class Main
{
    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) 
    {
        String  Token       = "430";
        String  replaceUUID = "-1";
        String  uuid        = "27";
        byte[] assignment   = null;


        //callReplaceAssignment(Token, uuid);

        try
        {
            uuid = callSubmitAssignment(Token);
            
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        try
        {
            replaceUUID = callReplaceAssignment(Token, uuid);
           

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

    }





    static private String callSubmitAssignment(String token)
    {
        ManagementMessage mMessage  = new ManagementMessage(ManagementMessage.SUBMIT_SERVICE, token);
        String  zipFileName = "c:/Users/Public/a6.zip";
        addZipAttachment(zipFileName, mMessage);
        return mMessage.call();
    }


    static private String callReplaceAssignment(String token, String uuid)
    {
        ManagementMessage mMessage  = new ManagementMessage(ManagementMessage.REPLACE_SERVICE, token);
        String  zipFileName = "c:/Users/Public/a7.zip";
        addZipAttachment(zipFileName, mMessage);
        mMessage.params().put(ManagementMessage.UUID_PARAMETER, uuid);
        return mMessage.call();
    }


	static private void addZipAttachment(String zipFilename, ManagementMessage message)
    {
		try
        {
        File f = new File(zipFilename);
        FileInputStream fileZip = new FileInputStream(f);
        byte b[] = new byte[(int)f.length()];

        int bytesRead;
        int remainingBytes = b.length;
        int offset = 0;

        while ( remainingBytes > 0 && -1 != (bytesRead = fileZip.read( b, offset, remainingBytes )) ){
          offset += bytesRead;
          remainingBytes -= bytesRead;
        }
        fileZip.read(b);
        message.setZip(b);


        /************ TEST CODE TEST CODE TEST CODE *************/
        TestAssignment assign = new TestAssignment();
        ZAssignmentArchivePackager packager = new ZAssignmentArchivePackager();
        FileOutputStream outFile = new FileOutputStream("c:/Users/Public/testassign.zip");
        outFile.write(packager.toByteArray(packager.pack(assign)));
        /************ TEST CODE TEST CODE TEST CODE *************/

		}
        catch (Exception e)
        {

			e.printStackTrace();
		}
	}
}



